package JAVA300.第十章IO;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 随机流学习
 * 读文件 存文件

 */

public class IO_Study16_RandomAccessFile {
    public static void main(String[] args) {
       //将整个文件计算有多少块，之后调用分块
        File file = new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA300/第十章IO/IO_Study14_ObjectInputStream.java");
        //总长度
        long length = file.length();
        //每块大小
        int blockSize = 1024;
        //块数
        int mount = (int) Math.ceil(length*1.0/blockSize);//*1.0是转double
        System.out.println(mount);//12

        //起始位置及大小
        int beginPos =0;
        int actualSize =0;
        for (int i=0;i<mount;i++){
            beginPos = i*mount;
            //实际大小考虑最后一块情况
            if(i == mount-1){
                actualSize = (int) length;

            }else {
                actualSize = blockSize;
                length -= actualSize;//已经用过了

            }
            System.out.println(i+"-->"+beginPos+"-->"+actualSize);
            //read2(i,beginPos,actualSize);
            read3(i,beginPos,actualSize);
        }




    }

    //指定起始位置，读取剩下所有
    public static void read1(){
        try {
            RandomAccessFile raf = new RandomAccessFile(new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA300/第十章IO/IO_Study14_ObjectInputStream.java"),"r");
            raf.seek(3);//从位置2开始,汉字第一个字符就会乱码 汉字对应3个字节

            int len = -1;
            byte[] flush = new byte[1024];
            while ((len = raf.read(flush)) != -1){
                System.out.println(new String(flush,0,len));
            }
            raf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //定起始位置，读取内容大于缓存区内容
    public static void read2(int i,int beginPos,int actualSize){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile((new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA300/第十章IO/IO_Study14_ObjectInputStream.java")),"r");
            raf.seek(beginPos);

            int len =-1;
            byte[] flush = new byte[1024];
           // int actualSize = 9;//每次读了一个汉字，读了三次
            while ((len = raf.read(flush)) != -1){
                if (actualSize > len){
                    System.out.println(new String(flush,0,len));
                    actualSize -= len;
                }else {
                    System.out.println(new String(flush,0,len));

                    break;

                }
            }
            raf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }



    }

    //在2的基础上进行分块文件的输出
    public static void read3(int i,int beginPos,int actualSize){
        RandomAccessFile raf = null;
        RandomAccessFile rafOut = null;
        try {

            raf = new RandomAccessFile((new File("/Users/hqqs/Documents/CodeJava/JavaSE_Exp/src/JAVA300/第十章IO/IO_Study14_ObjectInputStream.java")),"r");
            rafOut = new RandomAccessFile((new File(i+"study16CopyOut.java")),"rw");
            raf.seek(beginPos);

            int len =-1;
            byte[] flush = new byte[1024];
            // int actualSize = 9;//每次读了一个汉字，读了三次
            while ((len = raf.read(flush)) != -1){
                if (actualSize > len){
                   // System.out.println(new String(flush,0,len));
                    rafOut.write(flush,0,len);
                    actualSize -= len;
                }else {
                //    System.out.println(new String(flush,0,len));
                    rafOut.write(flush,0,len);

                    break;

                }
            }
            rafOut.close();
            raf.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }


    }
}
